package com.ngkj.zmfptwebsocket.chatModule.controller;

import com.ngkj.zmfptCommon.common.bean.ChatMessage;
import com.ngkj.zmfptCommon.common.bean.CommonReslut;
import com.ngkj.zmfptCommon.common.util.ResultEnum;
import com.ngkj.zmfptwebsocket.chatModule.service.read.ChatReadService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ChenHN
 * @since 2021/11/29 creation
 **/
@RestController
public class ChatController {

    @Resource
    private ChatReadService chatReadService;


    @GetMapping("/getTalkingMessage")
    public CommonReslut<List<ChatMessage>> getTalkingMessage(String userId, String toUserId){
        List<ChatMessage> talkingMessage = chatReadService.getTalkingMessage(userId,toUserId);
        return new CommonReslut<>(ResultEnum.ok,talkingMessage);
    }


    /**
     * 查看更多聊天记录
     * @param userId
     * @param toUserId
     * @return
     */
    @GetMapping("/addTalkingMessage")
    public CommonReslut<List<ChatMessage>> addTalkingMessage(String userId,String toUserId){
        try {
            List<ChatMessage> chatMessageList = chatReadService.addTalkingMessage(userId, toUserId);
            return new CommonReslut<>(ResultEnum.ok,chatMessageList);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonReslut<>(ResultEnum.error,null);
        }
    }


    @GetMapping("/getMessageList")
    public CommonReslut getMessageList(Long userId){
        try {
            List<Map<String,Object>> messageList = chatReadService.getMessageList(userId);
            return new CommonReslut(ResultEnum.ok,messageList);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

}
